// callPhoneHandler 联系客服
function callPhoneHandler(params) {
    wx.makePhoneCall({
        phoneNumber: params,
    })
}
// showToast 显示提示信息函数
function showToast(message, icon) {
    wx.showToast({
        title: message,
        icon: icon
    })
}
// 
function showModal(showtitle, showconttent, callback) {
    wx.showModal({
        title: showtitle,
        content: showconttent,
        success: function (res) {
            callback(res.confirm)
        },
        fail: function () {
            showToast("网络出现问题，请重试", "none")
        }
    })
}
// 登录事件
function loginHandler(callback) {
    wx.login({
        success: res => {
            // console.log(res);
            if (res.code) {
                wx.setStorageSync("openId", res.code);
                wx.getUserInfo({
                    success: res => {
                        // console.log(res);

                        wx.setStorageSync("userMessage", {
                            avatar: res.userInfo.avatarUrl,
                            username: res.userInfo.nickName,
                            buttonText: "退出"
                        });
                        callback({
                            avatar: res.userInfo.avatarUrl,
                            username: res.userInfo.nickName,
                            buttonText: "退出"
                        });

                    }
                })
            }
        },
        fail: function () {
            // showToast(" 网络出现问题，请重试", "none")
        }
    })
};
// function getLatLntHandler(){}
// 地址获取
function getLatLntHandler(callback) {
    var latitude = null;
    var longitude = null;
    var speed = null;
    var accuracy = null;
    // wx.showLoading({
    //   title: '地图正在加载中。。。',
    // })
    wx.getLocation({
        type: 'gcj02',
        success(res) {
            latitude = res.latitude
            longitude = res.longitude
            speed = res.speed
            accuracy = res.accuracy;
            var locationString = res.latitude + "," + res.longitude;
            console.log(latitude, longitude);
            // console.log("获取用户地理位置",latitude, longitude, speed, accuracy);
            addressName(latitude + "," + longitude, (address) => {

                callback({
                    title: address,
                    address: address,
                    location: {
                        lat: latitude,
                        lng: longitude
                    }
                })
            })
        },
        fail: function () {
            // getLatLntHandler();
        },
        complete: function () { }
    })
}
// 获取地址
function addressName(params, call) {
    wx.request({
        url: 'https://apis.map.qq.com/ws/geocoder/v1/?l&get_poi=1', //根据经纬度转换成具体地址
        data: {
            "key": "EKJBZ-72L3P-FHXDL-VSLEP-JEAGJ-JTFSD",
            "location": params
        },
        method: 'GET',
        // header: {},
        success(res) {
            call(res.data.result.address)
        },
        fail: function () {
            // showToast('网络出现错误', "none")
        },
        complete: function () { }
    })
}
// 页面跳转
function intoPageHandler(params) {
    wx.navigateTo({
        url: params,
    })
}

function payHandler(e) {
    var money = '0.01';
    wx.request({
        url: 'https://cjapp.135k.com/index.php/api/Pay/rechargePay',
        method: 'get',
        data: {
            uid: wx.getStorageSync('uid'),
            money: money,
        },
        success: function (res, callback) {
            console.log('res', res)
            var prepay_id = res.data.weixin.package
            var pay_id = prepay_id.slice(10);
            wx.requestPayment({
                'timeStamp': '' + res.data.weixin.timeStamp + '',
                'nonceStr': '' + res.data.weixin.nonceStr + '',
                'package': '' + res.data.weixin.package + '',
                'signType': 'MD5',
                'paySign': '' + res.data.weixin.paySign + '',
                'success': function (res) {
                    wx.showToast({
                        title: '支付成功',
                        duration: 1500,
                        success: function (res) {
                            console.log('eres', res)
                            //支付成功后回调处理数据
                            wx.request({
                                url: 'https://cjapp.135k.com/index.php/api/Pay/callback',
                                method: "get",
                                data: {
                                    uid: wx.getStorageSync('uid'),
                                    openid: wx.getStorageSync('openid'),
                                    pay_id: pay_id,
                                },
                                success: function (res) {
                                    console.log('resresres', res)
                                    callback(res)
                                }
                            })
                        }
                    })
                },
                'fail': function (res) {
                    // wx.navigateTo({
                    //   url: '../smallclass/index',
                    // })
                }
            })
        }
    })
}
// 获取位置路线
function getPolyline(callback) {
    const startAddress = wx.getStorageSync("startAddress");
    const endAddress = wx.getStorageSync("endAddress");
    console.log('获取路线列表', endAddress);
    const froms = startAddress.location.lat + "," + startAddress.location.lng + "&to=" + endAddress.location.lat + "," + endAddress.location.lng
    wx.request({
        url: `https://apis.map.qq.com/ws/direction/v1/driving/?from=${froms}&key=EKJBZ-72L3P-FHXDL-VSLEP-JEAGJ-JTFSD`,
        header: {
            'content-type': 'application/json' // 默认值
        },
        success: function (res) {

            if (res.data.status === 384) { } else {
                var coors = res.data.result.routes[0].polyline;
                for (var i = 2; i < coors.length; i++) {
                    coors[i] = coors[i - 2] + coors[i] / 1000000
                }
                // console.log(coors);
                const points = [];
                for (var j = 0; j < coors.length; j += 2) {
                    points[points.length] = {
                        longitude: coors[j + 1],
                        latitude: coors[j]
                    }
                }
                callback(points);
            }
        },
        fail: function () {
            // showToast('网络出现错误', "none")
        }
    })
}
// phoneTest 电话号码验证
function regTest(params, message, reg) {
    if (reg !== undefined) {
        if (params == '' || !reg.test(params)) {
            showToast(message, "none")
            return false;
        } else {
            return true;
        }
    } else {
        if (params == '' || params == undefined) {
            showToast(message, "none")
            return false;
        } else {
            return true;
        }
    }

}
//倒计时 倒数
function countDown(params, callback) {
    var seconds = 59;
    var countTimer = setInterval(function () {
        seconds--;
        callback(params + "s");
        if (seconds <= 0) {
            countTimer = clearInterval(countTimer);
            callback("获取验证码");
        }
    }, 1000);
}
// 验证银行卡号
function bankTest(bankno) {
    var lastNum = bankno.substr(bankno.length - 1, 1); //取出 最后一位（与 luhm 进行比较） 
    var first15Num = bankno.substr(0, bankno.length - 1); // 前 15 或 18 位 
    var newArr = new Array();
    for (var i = first15Num.length - 1; i > -1; i--) {
        newArr.push(first15Num.substr(i, 1));
    }
    var arrJiShu = new Array(); //奇数位*2 的积 <9 
    var arrJiShu2 = new Array(); //奇数位*2 的积 >9 
    var arrOuShu = new Array(); //偶数位数组 
    for (var j = 0; j < newArr.length; j++) {
        if ((j + 1) % 2 == 1) {
            //奇数位 
            if (parseInt(newArr[j]) * 2 < 9) arrJiShu.push(parseInt(newArr[j]) * 2);
            else arrJiShu2.push(parseInt(newArr[j]) * 2);
        } else arrOuShu.push(newArr[j]);
    }
    var jishu_child1 = new Array(); //奇数位*2 >9 的分割之 后的数组个位数 
    var jishu_child2 = new Array(); //奇数位*2 >9 的分割之 后的数组十位数 
    for (var h = 0; h < arrJiShu2.length; h++) {
        jishu_child1.push(parseInt(arrJiShu2[h]) % 10);
        jishu_child2.push(parseInt(arrJiShu2[h]) / 10);
    }
    var sumJiShu = 0; //奇数位*2 < 9 的数组之和 
    var sumOuShu = 0; //偶数位数组之和 
    var sumJiShuChild1 = 0; //奇数位*2 >9 的分割之后的数组 个位数之和 
    var sumJiShuChild2 = 0; //奇数位*2 >9 的分割之后的数组 十位数之和 
    var sumTotal = 0;
    for (var m = 0; m < arrJiShu.length; m++) {
        sumJiShu = sumJiShu + parseInt(arrJiShu[m]);
    }
    for (var n = 0; n < arrOuShu.length; n++) {
        sumOuShu = sumOuShu + parseInt(arrOuShu[n]);
    }
    for (var p = 0; p < jishu_child1.length; p++) {
        sumJiShuChild1 = sumJiShuChild1 + parseInt(jishu_child1[p]);
        sumJiShuChild2 = sumJiShuChild2 + parseInt(jishu_child2[p]);
    }
    //计算总和 
    sumTotal = parseInt(sumJiShu) + parseInt(sumOuShu) + parseInt(sumJiShuChild1) + parseInt(sumJiShuChild2);
    //计算 Luhm 值 
    var k = parseInt(sumTotal) % 10 == 0 ? 10 : parseInt(sumTotal) % 10;
    var luhm = 10 - k;
    if (lastNum == luhm) {
        return true;
    } else {
        showToast("请输入正确的银行卡号", "none")
        console.log("验证失败");
        return false;
    }
}
// 
function IdCardTest(idCard) {
    //15位和18位身份证号码的正则表达式
    var regIdCard = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
    //如果通过该验证，说明身份证格式正确，但准确性还需计算
    if (regIdCard.test(idCard)) {
        if (idCard.length == 18) {
            var idCardWi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); //将前17位加权因子保存在数组里
            var idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2); //这是除以11后，可能产生的11位余数、验证码，也保存成数组
            var idCardWiSum = 0; //用来保存前17位各自乖以加权因子后的总和
            for (var i = 0; i < 17; i++) {
                idCardWiSum += idCard.substring(i, i + 1) * idCardWi[i];
            }
            var idCardMod = idCardWiSum % 11; //计算出校验码所在数组的位置
            var idCardLast = idCard.substring(17); //得到最后一位身份证号码
            //如果等于2，则说明校验码是10，身份证号码最后一位应该是X
            if (idCardMod == 2) {
                if (idCardLast == "X" || idCardLast == "x") {
                    return true;
                } else {
                    showToast("身份证号码错误!", "none")
                    return false;
                }
            } else {
                //用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
                if (idCardLast == idCardY[idCardMod]) {
                    return true;
                } else {
                    showToast("身份证号码错误!", "none")
                    return false;
                }
            }
        }
    } else {
        showToast("身份证格式不正确！", "none")
        return false;
    }
}

function selectTimeHandler(dateParams, hoursParams, minutesParams) {
    console.log(dateParams);
    const data = new Date();
    const hour = data.getHours();
    const hours = [];
    const minute = [];
    console.log(dateParams, hoursParams, minutesParams);
    if (dateParams === 0 && hoursParams === 0) {
        hours.push("现在");
        for (var i = hour + 1; i < 24; i++) {
            hours.push(i + "点");
        }
        return {
            hours
        }
    } else if (dateParams === 0 && hoursParams !== 0) {
        hours.push("现在");
        for (var i = hour + 1; i < 24; i++) {
            hours.push(i + "点");
        }
        for (var i = 0; i < 60; i += 10) {
            minute.push(i);
        }
        return {
            hours,
            minute
        };
    } else if (dateParams !== 0) {
        for (var i = 0; i < 24; i++) {
            hours.push(i + "点");
        }
        for (var i = 0; i < 60; i += 10) {
            minute.push(i);
        }
        return {
            hours,
            minute
        };
    }
}
module.exports = {
    getLatLntHandler: getLatLntHandler,
    intoPageHandler: intoPageHandler,
    loginHandler: loginHandler,
    payHandler: payHandler,
    getPolyline: getPolyline,
    callPhoneHandler: callPhoneHandler,
    showToast: showToast,
    showModal: showModal,
    regTest: regTest,
    bankTest: bankTest,
    IdCardTest: IdCardTest,
    selectTimeHandler: selectTimeHandler,
    userImg: 'https://run.4hl.cn/addons/sd_drviing/core/public/WeChat/user/',
    driverImg: 'https://run.4hl.cn/addons/sd_drviing/core/public/WeChat/driver/',
}